package com.iexbuy.ihg.ui.fragment;

import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.ShopItemListAdapter;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.common.ExtraKeyConstant;
import com.iexbuy.ihg.entity.BusinessType;
import com.iexbuy.ihg.entity.SearchHistory;
import com.iexbuy.ihg.entity.ShopItemListObject;
import com.iexbuy.ihg.entity.ShopItemListObjectResponse;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class XiaoFeiSearchProductResultFragment extends BaseFragment {

	private static XiaoFeiSearchProductResultFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListLayout;
	@ViewInject(R.id.listView)
	private ListView listView;
	@ViewInject(R.id.currentCity)
	private EditText currentCity;
	@ViewInject(R.id.cityLayout)
	private LinearLayout cityLayout;
	@ViewInject(R.id.distictLayout)
	private LinearLayout distictLayout;

	private LinearLayout listHeaderView;

	private List<String> hotSearchTexts;

	private List<ShopItemListObject> shopItemListObjects = new ArrayList<ShopItemListObject>();
	private int pageNo = 0;
	private ShopItemListAdapter shopsAdapter;

	private ArrayAdapter<String> adapter;

	private List<SearchHistory> searchHistorys;

	private static String search;
	private static String selectedBusinessTypeCode;
	private String zoomCode;
	private static String distanceCode;
	private String cityCode;



	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_xiaofei_search_result, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.title_xiao_fei_search));

		msgListLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				refreshDate();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				loadMore();
			}
		});

		refreshDate();
		initDistanceView();
		initCategory();
	}

	//刷新整个页面数据
	private void refreshDate() {
		getSearchShops();
	}

	private void getSearchShops() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("latitude", CommUtil.getLatitude());
		paramMap.put("longitude", CommUtil.getLongitude());
		paramMap.put("pageNo", String.valueOf(pageNo));
		paramMap.put("pageSize", String.valueOf(ExtraKeyConstant.COMMON_PAGE_SIZE));
		if (selectedBusinessTypeCode != null) {
			paramMap.put("businessType", selectedBusinessTypeCode);
		}
		if (search != null) {
			paramMap.put("keyword", search);
		}
		if (zoomCode != null) {
			paramMap.put("area", zoomCode);
		}
		if (distanceCode != null) {
			paramMap.put("district", distanceCode);
		}
		if (cityCode != null) {
			paramMap.put("cityCode", cityCode);
		}
		ServiceHelper.send(mBaseActivity, ServiceHelper.SEARCH_SHOP, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
					}

					@Override
					public void onCancelled() {
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						msgListLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
						ShopItemListObjectResponse rep = JsonUtils.parseJsonStrToObject(responseInfo.result, ShopItemListObjectResponse.class);
						if (pageNo == 0) {
							shopItemListObjects.clear();
						}
						shopItemListObjects.addAll(rep.getBody());
						initShopListView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						msgListLayout.refreshFinish(PullToRefreshLayout.FAIL);
					}
				});
	}

	private void loadMore() {
		pageNo ++;
		getSearchShops();
	}

	private void initShopListView() {
		if (shopsAdapter == null) {
			shopsAdapter = new ShopItemListAdapter(mBaseActivity, shopItemListObjects);
			listView.setAdapter(shopsAdapter);
		}
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				ShopItemDetailFragment.setShopItemListObject(shopItemListObjects.get(i));
				CommUtil.openSingleFragmentActivity(mBaseActivity, ShopItemDetailFragment.class);
			}
		});
		shopsAdapter.notifyDataSetChanged();
	}

	private void initDistanceView () {
//		currentCity.setText(App.sPreferences.getCityName());
//		currentCity.setOnClickListener(new View.OnClickListener() {
//			@Override
//			public void onClick(View view) {
//				if (cityLayout.getVisibility() == View.GONE) {
//					cityLayout.setVisibility(View.VISIBLE);
//				} else {
//					cityLayout.setVisibility(View.GONE);
//				}
//			}
//		});
//		distictLayout.removeAllViews();
//		TextView textView;
//		for (DistrictObject districtObject : HomeFragment.getDistrictObjects()) {
//			textView = (TextView) mBaseActivity.getLayoutInflater().from(mBaseActivity).inflate(R.layout.fragment_xiaofei_district_hot_item, null);
//			textView.setText(districtObject.getName());
//			textView.setTag(districtObject.getCode());
//			textView.setOnClickListener(new View.OnClickListener() {
//				@Override
//				public void onClick(View view) {
//					distanceCode = view.getTag().toString();
//					getSearchShops();
//					cityLayout.setVisibility(View.GONE);
//				}
//			});
//		}
	}

	private void initCategory() {
		final ArrayList<String> businessTypeNames = new ArrayList<String>();
		for(BusinessType bt : HomeFragment.getBusinessTypes()) {
			businessTypeNames.add(bt.getName());
		}
	}


	public static void setSelectedBusinessTypeCode(String selectedBusinessTypeCode) {
		XiaoFeiSearchProductResultFragment.selectedBusinessTypeCode = selectedBusinessTypeCode;
	}

	public static void setSearch(String search) {
		XiaoFeiSearchProductResultFragment.search = search;
	}

	public static void setDistanceCode(String distanceCode) {
		XiaoFeiSearchProductResultFragment.distanceCode = distanceCode;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static XiaoFeiSearchProductResultFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
